package com.atguigu.gmall.config;

import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.WebFilter;

@Configuration
public class CorsConfig {

    //  编写一个webFilter 将这个对象直接注入到spring 容器就行了.
    @Bean
    public WebFilter webFilter(){
        //  创建corsConfiguration 对象
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");    //  允许跨域的url
        corsConfiguration.addAllowedMethod("*");    //  允许跨域的方式
        corsConfiguration.addAllowedHeader("*");    //  允许跨域携带请求头
        corsConfiguration.setAllowCredentials(true);    //  允许携带cookie 数据
        //  UrlBasedCorsConfigurationSource
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        //  CorsConfiguration
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        //  CorsConfigurationSource
        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }


}
